#ifndef CHATWINDOW_H
#define CHATWINDOW_H

#include <QDialog>
#include <QTimer>
#include "../../../Common/models.h"
#include "../../../Common/protocol.h"

namespace Ui {
class ChatWindow;
}

class ChatWindow : public QDialog
{
    Q_OBJECT

public:
    explicit ChatWindow(int currentUserId, int chatWithUserId, 
                       const QString& chatWithName, const QString& chatWithInfo = "", 
                       QWidget *parent = nullptr);
    ~ChatWindow();

protected:
    bool eventFilter(QObject *obj, QEvent *event) override;
    void closeEvent(QCloseEvent *event) override;
    void hideEvent(QHideEvent *event) override;
    void showEvent(QShowEvent *event) override;

private slots:
    void on_sendButton_clicked();
    void onMessageReceived(MessageType type, const QJsonObject& data);
    void refreshMessages();           // 定时刷新消息
    void onInputTextChanged();        // 输入框文本变化

private:
    Ui::ChatWindow *ui;
    int m_currentUserId;             // 当前用户ID
    int m_chatWithUserId;            // 聊天对象ID
    QString m_chatWithName;          // 聊天对象姓名
    QString m_chatWithInfo;          // 聊天对象额外信息（职称、科室等）
    QTimer* m_refreshTimer;          // 刷新定时器
    
    void loadChatHistory();          // 加载聊天历史
    void displayMessage(const Message& msg);  // 显示单条消息
    void sendMessage();              // 发送消息
    void markAsRead();               // 标记已读
};

#endif // CHATWINDOW_H

